<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      class="post-btn"
      @click="onPost"
      :disabled='!message'
    >发布</van-button>
  </div>
</template>

<script>
import { postCommentAPI } from '@/api/article.js' // 提交评论的接口
import { Toast } from 'vant' // 状态提示
export default {
  name: 'CommentPost',
  components: {},
  props: {
    // 文章id 或 评论id
    target: {
      type: [Number, Object, String],
      required: true
    },
    // 文章id
    artId: {
      type: [Number, Object, String],
      default: null
    },
    list: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      // 评论内容
      message: ''
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    // 提交评论的处理函数
    async  onPost () {
      Toast.loading({
        message: '加载中',
        forbidClick: true,
        duration: 0 // 持续展示
      })
      try {
        // 可能id是大数 所以加+''
        const { new_obj: res } = await postCommentAPI({ target: this.target + '', content: this.message, art_id: this.artId })
        // console.log(res)

        Toast.success('评论成功')
        this.$emit('onPostComment', res)
        this.message = ''
      } catch (error) {
        Toast.fail('评论失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
